<?php
/**
 * 获取推荐栏目数组的文章列表
 */
function get_lefthelp()
{
    $recmenu = '4,5,6,7,8,9';
    $sql = 'SELECT cat_id,cat_name,jump_url FROM '.$GLOBALS['ecs']->table('article_cat').' WHERE cat_id IN ('.$recmenu.') ORDER BY sort_order';
    $result = $GLOBALS['db']->getAll($sql);
    foreach ($result as $key => $val) {
        $article_id = $val['cat_id'];
        $result[$key]['url'] = empty($val['jump_url']) ? build_uri('article_cat', array('acid'=>$article_id), $result['title']) : $val['jump_url'];
        $sql = 'SELECT article_id,title FROM '.$GLOBALS['ecs']->table('article').' WHERE cat_id = '.$val['cat_id'].' AND article_type = 1';
        $rows = $GLOBALS['db']->getAll($sql);

        foreach($rows as $k => $v)
        {
            $result[$key]['articles'][$k]['article_id'] = $v['article_id'];
            $result[$key]['articles'][$k]['title']      = $v['title'];
            $result[$key]['articles'][$k]['url']        =  build_uri('article',array('aid'=>$v['article_id']));
        }
    }
    return $result;
}

/**
 * 获取产品目录
 * @param $catid
 * @return array
 */
function category_goods($catid)
{
    $sql = 'SELECT * FROM '.$GLOBALS['ecs']->table('category').' WHERE parent_id = '.$catid;
    $res = $GLOBALS['db']->query($sql);
    $arr = array();
    if($res)
    {
        while($row = $GLOBALS['db']->fetchRow($res))
        {
            $cat_id = $row['cat_id'];
            $arr[$cat_id]['cat_name'] = $row['cat_name'];
            $arr[$cat_id]['url'] = build_uri('category',array('cid'=>$cat_id), $row['cat_name']);
        }
    }
    return $arr;
}

/** 获取指定新闻列表
 * @param $cat_id
 * @param int $page
 * @param int $size
 * @param int $arctype
 * @return array
 */
function get_news($cat_id, $page = 1, $size = 10, $arctype=0 )
{
    //取出所有非0的文章
    $sql = 'SELECT article_id, title, author, description, file_url, add_time, file_url, open_type' .
        ' FROM ' .$GLOBALS['ecs']->table('article') .
        ' WHERE is_open = 1 AND cat_id = '.$cat_id.
        ' AND article_type = '.$arctype.' ORDER BY article_type DESC, article_id DESC';

    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page-1) * $size);

    $arr = array();
    if ($res)
    {
        while ($row = $GLOBALS['db']->fetchRow($res))
        {
			$article_id                      = $row['article_id'];
			$arr[$article_id]['id']          = $article_id;
			$arr[$article_id]['title']       = $row['title'];
			$arr[$article_id]['file_url']    = $row['file_url'];
			$arr[$article_id]['description'] = $row['description'];
			$arr[$article_id]['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ? sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']) : $row['title'];
			$arr[$article_id]['author']      = empty($row['author']) || $row['author'] == '_SHOPHELP' ? $GLOBALS['_CFG']['shop_name'] : $row['author'];
			$arr[$article_id]['url']         = $row['open_type'] != 1 ? build_uri('article', array('aid'=>$article_id), $row['title']) : trim($row['file_url']);
			$arr[$article_id]['add_time']    = date($GLOBALS['_CFG']['date_format'], $row['add_time']);
        }
    }
    return $arr;
}

/**获取专题列表
 * @param int $page
 * @param int $size
 * @return array
 */
function special_list($page=1, $size = 10)
{
    $sql = 'SELECT topic_id, title, topic_img, title_pic, description FROM '.$GLOBALS['ecs']->table('topic').' ORDER BY topic_id DESC ';
    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page-1) * $size);
    $arr = array();
    if($res)
    {
        while($row = $GLOBALS['db']->fetchRow($res))
        {
            $topicid = $row['topic_id'];
            $arr[$topicid]['title'] = $row['title'];
            $arr[$topicid]['url']   = 'topic.php?topic_id='.$row['topic_id'];
            $arr[$topicid]['thumb'] = $row['topic_img'];
            $arr[$topicid]['pic']   = $row['title_pic'];
            $arr[$topicid]['description'] = $row['description'];
        }
    }
    return $arr;
}

/**获取专题数量
 * @return bool|string
 */
function get_special_count()
{
    global $db, $ecs;
    $leftday = time();
    $count = $db->getOne("SELECT COUNT(*) FROM " . $ecs->table('topic') . " WHERE ". $leftday ." BETWEEN start_time AND end_time");
    return $count;
}

/**
 * 获取网站底部帮助文档
 * @return array
 */
function nav_bottom()
{
    $arr_cat = array(6,7,8,9,3);
    $arr = array();
    $index = 0;
    foreach($arr_cat as $k => $v)
    {
        $sql = 'SELECT article_id,title FROM '.$GLOBALS['ecs']->table('article').' WHERE cat_id = '.$v;
        $res = $GLOBALS['db']->selectLimit($sql,4,0);
        if($res)
        {
            $idx = 0;
            while($row = $GLOBALS['db']->fetchRow($res))
            {
                $arr[$index]['child'][$idx]['title'] = $row['title'];
                $arr[$index]['child'][$idx]['url']   = build_uri('article',array('aid'=>$row['article_id']));
                $idx++;
            }
        }
        $index++;
    }
    return $arr;
}
?>